#include "Category.h"

Category ::Category(){};
void Category :: setProfile(Profile user)
{
	profile = user;

}
void Category :: defaultScreen()
{
	int choice = 99;
	
	while(choice !=0){
		cout << "Please indicate what you would like to do? \n1) Display all logs \n2) Add new log \n3) Edit new log \n4) Delete log \n0) Quit " << endl;
		cin >> choice;
		if(choice == 1)
		{
			system("CLS");
			displayLoglist();
		}
		else if (choice == 2)
		{
			addNewLog();			
		}
		else if (choice == 3)
		{
			editLog();
			system("CLS");
		}
		else if (choice == 4)
		{
			deleteLog();
			system("CLS");
		}
	
		else if( choice == 0 )
		{
			system("CLS");
			break;
			
		}
		else
		{
			cout << "Invalid choice!" << endl;
			system("CLS");
		}
	}
}
void Category :: lineInput( string line) 
{

	stringstream ss;
	Log tempLog;
	Date tempDate;
	int tempDay, tempMonth, tempYear;
	double tempAmount;
	string tempComment;

	ss << line;
	ss >> tempDay;
	ss >> tempMonth;
	ss >> tempYear;
	ss >> tempAmount;
	ss >> tempComment;

	tempDate.setDay(tempDay);
	tempDate.setMonth(tempMonth);
	tempDate.setyear(tempYear);
	tempLog.setDate(tempDate);
	tempLog.setAmount(tempAmount);
	tempLog.setComment(tempComment);
	
	logList.push_back(tempLog);

}
void Category :: addNewLog()
{
	int valid = 1;
	string newLog;
	int tempDay = 0;
	int tempMonth = 0;
	int tempYear = 0;
	double tempAmount = 0;
	string tempComment = " ";
	stringstream ss;
	while(valid)
	{
		cout << "Please key in information according to \n(DD MM YYYY) (Amount) (Comment)  " << endl;
		cin.ignore(1000, '\n');
		getline(cin, newLog);
		ss << newLog;


		ss >> tempDay;
		ss >> tempMonth;
		ss >> tempYear;
		ss >> tempAmount;
		ss >> tempComment;
		if(tempDay == 0 || tempMonth == 0 || tempYear == 0 || tempAmount == 0 || tempComment == " ")
		{
			system("CLS");
			cout << "Missing Inputs, Please Try Again\n\n" << endl;
		}
		else
		{
			valid = 0;
			lineInput(newLog);
			system("CLS");
			cout << "Log added successfully!\n\n" << endl;
		}
	}

}
void Category :: addLog(string newLog)
{
	if (newLog.empty() != 1)
		lineInput(newLog);
}
void Category :: deleteLog()
{
	int logNum;
	cout << "Which log would you like to delete? " << endl;
	cin >> logNum;
	logList.erase(logList.begin() + logNum - 1);

}
void Category :: editLog()
{

	int logChoice;
	string logToEdit;
	cout << "Which log do you wish to edit? " << endl;
	cin >> logChoice;
	cout << "In this log which information do you wish to change? Date, Amount or Comment?" << endl;
	cin >> logToEdit ;
	
	if(logToEdit == "Date")
	{
		Date newDate;
		int Day, Month, Year;
		cout << " Please Enter Date in DD MM YYYY Format" << endl;
		cin >> Day;
		cin >> Month;
		cin >> Year;
		newDate.setDay(Day);
		newDate.setMonth(Month);
		newDate.setyear(Year);
		logList[logChoice - 1].setDate(newDate);
	}
	else if(logToEdit == "Amount")
	{
		double newAmount;
		cout << " Please Enter New Amount: " << endl;
		cin >> newAmount;
		logList[logChoice-1].setAmount(newAmount);
	}
	else if(logToEdit == "Comment")
	{
		string newComment;
		cout << "Please Enter New Comment: " << endl;
		cin.ignore(1000, '\n');
		cin >> newComment;
		logList[logChoice - 1].setComment(newComment);

	}
	else
	{
		cout << "Invalid choice! " << endl;
	}

}
void Category :: displayLoglist()
{
	if(logList.size() == 0)
		cout << "Log list is empty!\n\n";
	else{
		cout << "No.)\tDD/MM/YYYY\tAmount\tComment" << endl;
		for (size_t i = 0; i < logList.size(); i++)
		{
			cout << i+1 << ")\t" << logList[i].getDate().getDay() << "/" << logList[i].getDate().getMonth() << "/" << logList[i].getDate().getYear() << "\t" << logList[i].getAmount() << "\t" << logList[i].getComment() << endl;
		}
		cout << "\n" << endl;
	}
}
double Category :: getTotalExpenditure()
{
	double totalExpenditure = 0;
	
	for(size_t i = 0; i< logList.size(); i++)
		totalExpenditure += logList[i].getAmount();

	return totalExpenditure;
}
/*Log Category:: getSearchline(vector<Log> logList, string searchInput)
{
	for(int i; i <logList.size(); i++)
	{
		if(searchInput == logList[i].getAmount())
		{}
		else if (searchInput == logList[i].getComment())
		{}
		else if (searchInput == logList[i].getDate())
		{}
	}


}*/